<template>
  <div class="view-wrapper">
    <div class="head">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="商户账户名称：">
          <el-input
            v-model="formInline.merchantName"
            placeholder="请输入商户账户名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="店铺名称：">
          <el-input
            v-model="formInline.shopName"
            placeholder="请输入店铺名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="爆卡名称：">
          <el-input
            v-model="formInline.name"
            placeholder="请输入爆卡名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="爆卡分类：">
          <el-select v-model="formInline.type" placeholder="请选择">
            <el-option
              v-for="item in options3"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上下架状态：">
          <el-select v-model="formInline.shopStatus" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="爆卡状态：">
          <el-select v-model="formInline.status" placeholder="请选择">
            <el-option
              v-for="item in options2"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head">
        <el-button type="primary" @click="puchCard">发爆卡</el-button>
        <div class="">
          <el-button type="primary" @click="experienceReset"
            >批量上架</el-button
          >
          <el-button type="danger" @click="experienceStop">批量下架</el-button>
          <export-excel-button
            title="爆卡列表"
            action="/asset/experience/v1/assetCardList/export"
            :params="ExportParams"
          />
        </div>
      </div>
      <div class="total_box">
        总计<span>{{ total }}</span
        >条数据
      </div>
      <ex-table
        ref="table"
        :loading="loading"
        :table-column="tableColumn"
        :table-data="tableData"
        :showSelect="true"
        @selectChange="selectChange"
        @pageSizeChange="handleSizeChange"
        @currentPageChange="handleCurrentChange"
      >
        <template v-slot:operate="{ scope }">
          <el-button
            type="primary"
            v-if="scope.row.shopStatus != 1"
            @click="grounding(scope.row)"
            >上架</el-button
          >
          <el-button
            type="danger"
            v-if="scope.row.shopStatus == 1"
            @click="undercarriage(scope.row)"
            >下架</el-button
          >
          <el-button @click="lookDetail(scope.row)">编辑</el-button>
        </template>
      </ex-table>
    </div>
    <puchCard-dialog
      v-if="detailDialog3"
      :id="id"
      :visible.sync="detailDialog3"
      @success="_handleSuccess"
    />
  </div>
</template>
<script>
// components
import {
  assetCardList,
  adminDict,
  experienceStop,
  experienceReset,
} from "@/api/operateActive";
import ExportExcelButton from "@/components/ExportExcelButton/index";
import handlePageMixin from "@/mixins/handlePageMixin";
import puchCardDialog from "./components/puchCardDialog";
export default {
  name: "billList",
  components: {
    ExportExcelButton,
    puchCardDialog,
  },
  mixins: [handlePageMixin],
  data() {
    return {
      loading: false,
      detailDialog3: false,
      id: "",
      formInline: {
        merchantName: "", //商户账号名称
        shopName: "", //店铺名称
        name: "", //爆卡名称
        type: "", //爆卡分类
        shopStatus: "", //上下架状态
        status: "", //爆卡状态
      },
      date: [],
      options: [
        {
          id: "",
          value: "全部",
        },
        {
          id: "1",
          value: "在售",
        },
        {
          id: "2",
          value: "下架",
        },
      ],
      cardLs: [], //上下架操作数组
      options2: [],
      options3: [],
      tableData: [],
      total: "",
      tableColumn: [
        { prop: "merchantName", label: "商户账号名称" },
        { prop: "shopName", label: "店铺名称" },
        { prop: "name", label: "爆卡名称" },
        { prop: "typeDict", label: "爆卡分类" },
        { prop: "buyMoney", label: "售价/¥", width: 120 },
        { prop: "used", label: "已发售/张", width: 120 },
        { prop: "shopStatusDict", label: "上下架状态"},
        {prop: "statusDict",label: "爆卡状态",width: 120,},
        { prop: "createTime", label: "创建时间", width: 120 },
        { label: "操作", slot: true, slotName: "operate", width: 180  },
      ],
    };
  },
  computed: {
    ExportParams() {
      const { id: ticketId } = this;
      const params = {
        ...this.pageOptions,
        ...this.formInline,
        startTime: this.date[0],
        endTime: this.date[1],
      };
      return params;
    },
  },
  mounted() {
    this.getlinkagelist();
    this.getTransactiontype();
    this.getList();
  },
  methods: {
    getlinkagelist() {
      //获取爆卡分类
      const params = {
        dictType: "asset_experience_card_type",
      };
      adminDict(params).then((res) => {
        var arr = [
          {
            id: "",
            value: "全部",
          },
        ];
        this.options3 = arr.concat(res.result[0].dictList);
      });
    },
    getTransactiontype() {
      //爆卡状态
      const params = {
        dictType: "asset_status",
      };
      adminDict(params).then((res) => {
        var arr = [
          {
            id: "",
            value: "全部",
          },
        ];
        this.options2 = arr.concat(res.result[0].dictList);
      });
    },
    lookDetail(e) {
      console.log(e);
      this.$router.push({
        path: "/merchants/card-detail",
        query: {
          merchantId: e.merchantId,
          assetId: e.assetId,
          name: e.name,
        },
      });
    },
    search() {
      //搜索
      console.log(this.formInline);
      this.pageOptions.pageNum = 1;
      this.$refs.table.pageOptions.pageNum = 1;
      this.getList();
    },
    reset() {
      this.formInline = {};
      this.pageOptions.pageNum = 1;
      this.$refs.table.pageOptions.pageNum = 1;
      this.getList();
    },
    selectChange(e) {
      //表格多选
      let list = e;
      let newlist = list.map((item, index) => {
        return Object.assign(
          { merchantId: item.merchantId },
          { assetId: item.assetId }
        );
      });
      console.log(newlist);
      this.cardLs = newlist;
    },
    experienceStop() {
      //批量下架
      if (this.cardLs.length <= 0) {
        this.$confirm("请选择爆卡", "批量操作", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "warning",
        })
          .then(() => {})
          .catch(() => {});
      } else {
        const h = this.$createElement;
        this.$msgbox({
          title: "消息",
          message: h("p", null, [
            h("span", null, "确认将"),
            h("span", { style: "color: red" }, this.cardLs.length),
            h("span", null, "张爆卡批量上架/下架?"),
          ]),
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            const params = this.cardLs;
            experienceStop(params)
              .then((res) => {
                this.$message({
                  type: "success",
                  message: "下架成功!",
                });
                this.reset();
              })
              .finally(() => {
                this.loading = false;
              });
          })
          .catch(() => {});
      }
    },
    experienceReset() {
      //批量上架
      if (this.cardLs.length <= 0) {
        this.$confirm("请选择爆卡", "批量操作", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "warning",
        })
          .then(() => {})
          .catch(() => {});
      } else {
        const h = this.$createElement;
        this.$msgbox({
          title: "消息",
          message: h("p", null, [
            h("span", null, "确认将"),
            h("span", { style: "color: red" }, this.cardLs.length),
            h("span", null, "张爆卡批量上架/下架?"),
          ]),
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            const params = this.cardLs;
            experienceReset(params)
              .then((res) => {
                this.$message({
                  type: "success",
                  message: "上架成功!",
                });
                this.reset();
              })
              .finally(() => {
                this.loading = false;
              });
          })
          .catch(() => {});
      }
    },
    grounding(item) {
      //单个上架
      this.loading = true
      const params = [{ assetId: item.assetId, merchantId: item.merchantId }];
      experienceReset(params)
        .then((res) => {
          this.$message({
            type: "success",
            message: "上架成功!",
          });
          this.getList();
          this.loading = false
        })
        .finally(() => {
          this.loading = false;
        });
    },
    undercarriage(item) {
      this.loading = true
      //单个下架
      const params = [{ assetId: item.assetId, merchantId: item.merchantId }];
      experienceStop(params)
        .then((res) => {
          this.$message({
            type: "success",
            message: "下架成功!",
          });
          this.getList();
          this.loading = false
        })
        .finally(() => {
          this.loading = false;
        });
    },
    puchCard() {
      //发爆卡
      this.detailDialog3 = true;
    },
    _handleSuccess() {
      this.reset();
    },
    getList() {
      const params = {
        ...this.pageOptions,
        ...this.formInline,
      };
      this.loading = true;
      assetCardList(params)
        .then((res) => {
          const { total, records } = res.result;
          this.tableData = records;
          this.$refs.table.pageOptions.total = total;
          this.total = total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
  },
};
</script>
<style lang="scss">
.el-card-title {
  font-size: 20px;
}
</style>
<style lang="scss" scoped>
.head {
  box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
  background-color: #fff;
  border-radius: 8px;
  //   height: 120px;
  padding: 20px 20px 0;
}
.el-form--inline .el-form-item {
  margin-right: 40px;
}
.listBox {
  box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
  background-color: #fff;
  border-radius: 8px;
  //   height: 120px;
  padding: 20px 20px;
  margin-top: 10px;
}
.table_list {
  display: flex;
  align-items: center;
  // border: 1px solid #DCDFE6;
  border-radius: 7px;
  width: 220px;
  margin-bottom: 10px;
}
.tableBox {
  width: calc(100% / 3);
  height: 36px;
  text-align: center;
  line-height: 36px;
  color: #303133;
  font-size: 14px;
  border: 1px solid #dcdfe6;
  border-right: 0;
}
.tableBox:first-child {
  border-radius: 7px 0px 0px 7px;
}
.tableBox:last-child {
  border-right: 1px solid #dcdfe6;
  border-radius: 0px 7px 7px 0px;
}
.spchoosetext {
  background-color: #fff4e6;
  color: #ff9300;
  // border: 1px solid #ff9300;
}
.listBox_head {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.total_box {
  font-size: 16px;
  margin-bottom: 16px;
}
.total_box span {
  color: #ff4949;
  margin: 0 4px;
}
</style>
